<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
                <a-row>
                  <a-col :span="12">
            <a-form-model-item label="试题类型" prop="selectType" :rules="$valid.required" :maxLength="50">
                <a-select
                        :size="'default'"
                        style="min-width: 100px"
                        :showArrow="true"
                        v-model="formInfo.selectType"
                        placeholder="请选择">
                    <a-select-option v-for="(item) in this.$sysDictOptions('select_type')" :key="item.k" :value="item.k">
                        {{ item.v }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>
          </a-col>
              <a-col :span="12">
            <a-form-model-item label="是否必选" prop="isMust" :rules="$valid.required" :maxLength="50">

                <a-select
                        :size="'default'"
                        style="min-width: 100px"
                        :showArrow="true"
                        v-model="formInfo.isMust"
                        placeholder="请选择">
                    <a-select-option v-for="(item) in this.$sysDictOptions('whetherFlag')" :key="item.k" :value="item.k">
                        {{ item.v }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>
          </a-col>
            </a-row>
          <a-row>
              <a-col :span="24">
                  <a-form-model-item label="题目内容" prop="itemName" :labelCol="$global.labelTextAreaCol"  :wrapperCol="$global.wrapperTextAreaCol"  :rules="$valid.required" :maxLength="50">
                      <a-input v-model="formInfo.itemName" placeholder="请输入内容" type="textarea"/>
                  </a-form-model-item>
              </a-col>
          </a-row>
         <a-row>
              <a-col :span="24">
                  <a-form-model-item label="条件选项"  :labelCol="$global.labelTextAreaCol"  :wrapperCol="$global.wrapperTextAreaCol" :maxLength="50">
                      <a-input v-model="glname" placeholder="请选择" @click="showglid" />
                      <eva-question-glid ref="glid" @ok="setglid"></eva-question-glid>
                  </a-form-model-item>
              </a-col>
          </a-row>
        <a-button @click="addPlug" type="primary" icon="plus"  v-if="formInfo.selectType ==1 ||formInfo.selectType ==2">添加</a-button>
        <a-card style="margin-top: 10px" v-if="formInfo.selectType ==1 ||formInfo.selectType ==2">
            <a-row v-for="it in this.dslist">
                <a-col :span="2">
                    {{it.itemNum}}
                    <a-input v-model="it.itemNum"  type="radio" v-if="formInfo.selectType ==1"/>
                    <a-input v-model="it.itemNum"  type="checkbox" v-if="formInfo.selectType ==2"/>
                </a-col>
                <a-col :span="13">
                    <a-form-model-item label=""   :maxLength="50">
                        <a-input v-model="it.itemName" placeholder="请输入内容" type="textarea"/>
                    </a-form-model-item>
                </a-col>
                <a-col :span="6">
                    <a-form-model-item label="分值" :maxLength="50">
                        <a-input v-model="it.sorce"  placeholder="请输入分值"/>
                    </a-form-model-item>
                </a-col>
                <a-col :span="3">
                    <a @click="remove(it)">删除</a>
                </a-col>
            </a-row>


        </a-card>
          </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import EvaQuestionGlid from './EvaQuestionGlid'
  export default {
    name: 'EvaQuestionEdit',
    components: {
        EvaQuestionGlid
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
          dslist:[
              {itemNum:1},
              {itemNum:2}
          ],
        formInfo: {
          itemName: undefined,
          itemNum: undefined,
          parendid: undefined,
          selectType: undefined,
          isMust: undefined,
          sorce: undefined,
          infoid: undefined,
          createTime: undefined,
          wx: '',
            glid:''
        },
          glname:'',
        url: {
          saveInfo: '/system/evaQuestion/save',
          getInfo: '/system/evaQuestion/info'
        }
      }
    },
    created () {
    },
    methods: {
        showglid(){
            this.$refs.glid.show(this.formInfo.infoid )
        },
        setglid(val,v2){
            this.formInfo.glid = val
            this.glname = v2
            console.log(val)
            console.log(this.formInfo.glname)
        },
      add () {
        this.edit()
      },
      edit (record) {
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.formInfo = res.result.data
                console.log("res:",res)
              this.dslist = res.result.dslist
                this.glname = res.result.glname

            })
          })
        }
        this.visible = true
      },
        edits (record) {
            if (record) {
                this.formInfo.infoid = record
                this.dslist=[
                    {itemNum:1},
                    {itemNum:2}
                ]
                // getAction(this.url.getInfo, { 'infoid': record.id }).then(res => {
                //     this.$nextTick(() => {
                //         this.formInfo = res.result.data
                //     })
                // })
            }
            this.visible = true
        },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
              console.log(this.formInfo)
              const repObj = JSON.stringify(this.dslist)
              this.formInfo.dslist = repObj;
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
        addPlug(){
            var of ={};
            of.itemNum=this.dslist.length+1;
            this.dslist.push(of)
        },
        remove(item) {
            let index = this.dslist.indexOf(item);
            if (index !== -1) {
                this.dslist.splice(index, 1);
            }
            this.dslist.forEach(function (item, i) {
                item.itemNum =i+1
            })
        },
            // 关闭
      handleCancel () {
        this.close()
      },
      close () {
          this.dslist=[]
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.visible = false
      },
      editorOk (val) {
        this.formInfo.content = val
      }
    }
  }
</script>
